package com.carpool.finder;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class GoogleMapActivity extends MapActivity {

private MapView mapView;
private MapController mc;
//MyLocationOverlay myLocOverlay = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);

mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
mapView.setBuiltInZoomControls(true);

Drawable makerDefault = this.getResources().getDrawable(R.drawable.star_big_on);
MirItemizedOverlay itemizedOverlay = new MirItemizedOverlay(makerDefault);
GeoPoint point = new GeoPoint(33480000, 73000000);

OverlayItem overlayItem = new OverlayItem(point, "Islamabad", null);
itemizedOverlay.addOverlayItem(33695043, 73000000, "Islamabad");
itemizedOverlay.addOverlayItem(33480000, 73000000, "Some Other Pakistani City");
itemizedOverlay.addOverlayItem(33380000, 73000000, "Some Other Pakistani City");

mapView.getOverlays().add(itemizedOverlay);

MapController mc = mapView.getController();
mc.setCenter(new GeoPoint(33580000, 73000000)); // Some where near Islamabad.
mc.zoomToSpan(itemizedOverlay.getLatSpanE6(), itemizedOverlay.getLonSpanE6());

}

@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}

private class MirItemizedOverlay extends ItemizedOverlay<OverlayItem> {

private List<OverlayItem> mOverlays = new ArrayList<OverlayItem>();

public MirItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
// TODO Auto-generated constructor stub
}
@Override
protected OverlayItem createItem(int i) {
return mOverlays.get(i);
}

@Override
public int size() {
return mOverlays.size();
}

public void addOverlayItem(OverlayItem overlayItem) {
mOverlays.add(overlayItem);
populate();
}

public void addOverlayItem(int lat, int lon, String title) {
GeoPoint point = new GeoPoint(lat, lon);
OverlayItem overlayItem = new OverlayItem(point, title, null);
addOverlayItem(overlayItem);
}

@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView)
{
//---when user lifts his finger---
if (event.getAction() == 1) {
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
Toast.makeText(getBaseContext(),p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6 ,Toast.LENGTH_SHORT).show();

GeoPoint point = new GeoPoint((int)p.getLatitudeE6(),(int)p.getLongitudeE6());
String address = ConvertPointToLocation(point);
String data = p.getLatitudeE6() / 1E6 + "," +p.getLongitudeE6() /1E6+","+address;

Intent i = getIntent();
String msg = i.getStringExtra("command");
if(msg.contentEquals("start")){
	i.putExtra("locationstart", data);
	setResult(RESULT_OK, i);
	finish();
}

if(msg.contentEquals("end")){
	i.putExtra("locationend", data);
	setResult(RESULT_OK, i);
	finish();
}

if(msg.contentEquals("via")){
	i.putExtra("locationvia", data);
	setResult(RESULT_OK, i);
	finish();
}



}
return false;
}

}

public String ConvertPointToLocation(GeoPoint point) {   
    String address = "";
    Geocoder geoCoder = new Geocoder(
            getBaseContext(), Locale.getDefault());
    try {
        List<Address> addresses = geoCoder.getFromLocation(
            point.getLatitudeE6()  / 1E6, 
            point.getLongitudeE6() / 1E6, 1);

        if (addresses.size() > 0) {
            for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                address += addresses.get(0).getAddressLine(index) + " ";
        }
    }
    catch (IOException e) {                
        e.printStackTrace();
    }   

    return address;
} 

}